package cn.push.android;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;



import cn.push.embed.core.PushManager;
import cn.push.embed.core.utils.Logger;
import cn.push.embed.core.utils.Setting;
import cn.push.embed.utils.CommonUtils;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button btRegister,btLogout,btServer;
    private EditText etUserId;
    private TextView tvLog;
    public static Handler handler;
    private Setting setting;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btRegister= (Button) this.findViewById(R.id.button);
        btLogout= (Button) this.findViewById(R.id.button2);
        btServer= (Button) this.findViewById(R.id.button3);
        etUserId= (EditText) this.findViewById(R.id.tv_user_id);
        tvLog= (TextView) this.findViewById(R.id.tv_log);
        btRegister.setOnClickListener(this);
        btLogout.setOnClickListener(this);
        btServer.setOnClickListener(this);
        setting= new Setting(this, getPackageName());
        String alias = setting.loadString("alias");
        etUserId.setText(alias);



        tvLog.setMovementMethod(ScrollingMovementMethod.getInstance());

        handler=new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                String message;
                message=msg.getData().getString("message");

                Logger.showLogToText(tvLog,message);
                return false;
            }
        });


    }



    @Override
    public void onClick(View v) {
        String userId=etUserId.getText().toString().trim();
        switch (v.getId()){
            case R.id.button:
                if (CommonUtils.isEmpty(userId)){
                    Toast.makeText(this,"userId 不能为空",Toast.LENGTH_SHORT).show();
                    return;
                }
                    PushManager.getInstance().setAlias(userId);
                break;
            case R.id.button2:
                PushManager.getInstance().setAlias("");
                break;
            case R.id.button3:
               /* PushService.serverAlias();*/
                break;
        }
    }
}
